<%@ page contentType="text/html;charset=UTF-8" language="java"
	pageEncoding="utf-8"%>
<%@ include file="../include/head.jsp"%>
<%@ include file="../include/top.jsp"%>
<%@ include file="../include/left.jsp"%>
<div class="main-content">

	<%@ include file="../include/navigate.jsp" %>
	
	<div class="container">
		<div class="widget-box">
			<div class="widget-box-header">
				<div class="col-sm-2">
					<div class="title">
						<i class="icon-edit">&nbsp;</i>编辑用户权限
					</div>
				</div>
			</div>
			<div class="widget-box-content" style="padding: 15px;">
				<c:if test="${not empty message}">
					<div class="alert alert-danger" role="alert">${message}</div>
				</c:if>
				<div id="saveStatus"></div>
				<form class="form-horizontal" role="form"
					action="${pageContext.request.contextPath}/user/edit" method="post">
					<input type="hidden" name="id" value="${user.id}" />

					<div class="form-group">
						<label class="col-sm-2 control-label">权限设置:</label>
						<div class="col-sm-8">
							<table class="table table-hover table-bordered">
								<tbody>
								
									<tr>
										<td><strong>菜单项</strong></strong></td>
										<td><strong>可执行操作</strong></strong></td>
									</tr>

									<c:forEach items="${permissionArray}" var="permission">
										<c:set var="isPrint" value="true"></c:set>
										<c:forEach items="${permission.value}" var="subPermission">
											<tr>
												<c:if test="${isPrint}">
													<c:set var="isPrint" value="false"></c:set>
													<td rowspan="${permission.value.size()}"><input
														type="checkbox" name="${permission.key}"
														onclick="cascadeSub(this)"></input>&nbsp;&nbsp;${permission.key}</td>
												</c:if>
												<c:set var="permissionArray"
													value="${subPermission.code.split(':')}"></c:set>
												<c:set var="permissionArrayLength"
													value="${fn:length(permissionArray)}"></c:set>
												<c:set var="printChecked"
													value="${(subPermission.enable=='true')? 'checked':''}"></c:set>
												<c:set var="printDisable"
													value="${(subPermission.editable=='false')? 'disabled':''}"></c:set>
												<td><input type="checkbox"
													value="${subPermission.code}" name="${permission.key}_sub"
													${printChecked} ${printDisable}
													onclick="cascadeParent(this)">&nbsp;&nbsp;${permissionArray[permissionArrayLength - 1]}</input></td>
											</tr>
										</c:forEach>
									</c:forEach>
								</tbody>
							</table>
						</div>
					</div>

					<div class="form-group">
						<label class="col-sm-2 control-label"></label>

						<div class="col-sm-2">
							<button type="button" class="btn btn-success" onclick="submitUserData()">保存</button>
							<a type="button" class="btn btn-primary"
								href="${pageContext.request.contextPath}/user/list/1"> 返回列表
							</a>
						</div>
					</div>
				</form>
			</div>
		</div>
		<div style="position: fixed; bottom: 0px; width: 100%;" align="center">
			<%@ include file="../include/footer.jsp"%>
		</div>
	</div>

	<script type="text/javascript">
		$(document)
				.ready(
						function() {
							function setParentStatus(parentNode) {
								var parentName = $(parentNode).attr("name");
								var parentNode = $(":checkbox[name="
										+ parentName + "]");
								var childrenNodes = $(":checkbox[name="
										+ parentName + "_sub]");

								var childrenNum = childrenNodes.length;
								var checkedNum = 0;
								var disableNum = 0;
								$.each(childrenNodes, function(index, value) {
									if (true == $(value).prop("checked")) {
										checkedNum++;
									}

									if (true == $(value).prop("disabled")) {
										disableNum++;
									}
								});

								if (checkedNum == 0) {
									parentNode.prop("checked", false);
									parentNode.prop("indeterminate", false);
								} else if (checkedNum > 0
										&& checkedNum < childrenNum) {
									parentNode.prop("indeterminate", true);
								} else {
									parentNode.prop("indeterminate", false);
									parentNode.prop("checked", true);
								}

								if (disableNum > 0) {
									parentNode.prop("disabled", true);
								}
							}

							function InitParentStatus() {
								parentNodes = $(":checkbox").not(
										":checkbox[name$='_sub']"); // select all parent node
								$.each(parentNodes, function(index, value) {
									setParentStatus(value);
								});
							}

							InitParentStatus();

							window.cascadeSub = function(parentNode) {
								var parentName = $(parentNode).attr("name");
								var parentStatus = $(parentNode)
										.prop("checked");
								var childrenName = parentName + "_sub";

								if (true == parentStatus) { // if parent node checked? if checked, checked sub nodes
									$.each($(":checkbox[name=" + childrenName
											+ "]"), function(index, value) {
										$(value).prop("checked", true);
									});
								} else {
									$.each($(":checkbox[name=" + childrenName
											+ "]"), function(index, value) {
										$(value).prop("checked", false);
									});
								}
							};

							window.cascadeParent = function(childNode) {
								var childName = $(childNode).attr("name");
								var parentName = childName.replace("_sub", "");
								var parentNode = $(":checkbox[name="
										+ parentName + "]");

								setParentStatus(parentNode);
							};

							window.submitUserData = function() {

								var permissions = "";
								$.each($(":checked[name$='_sub']").not(":disabled"), function(
										index, value) {
									permissions += $(value).val() + ";";
								});

 								$
										.ajax({
											type : 'post',
											url : '/user/savePrivilege',
											data : {
												"id" : $("input[name='id']").val(),
												"code" : encodeURI(permissions)
											},
											cache : false,
											dataType : 'json',
											success : function(data) {
												if(data == 'success'){
													operationSuccess();
												} else {
													operationFailedWithMessage(data);
												}
											},
											error : function() {
												operationFailed();
											}
										}); 

							}
						});
	</script>

	<%@ include file="../include/bottom.jsp"%>